<?php

namespace app\controller;

use app\service\MemberPer;
use support\Request;
use app\service\Item as ItemService;
use think\Exception;

class Item
{

    private $item;

    public function __construct(ItemService $item)
    {
        $this->item = $item;
    }

    /**
     * 创建项目
     * @param Request $request
     * @return \support\Response
     */
    public function create(Request $request)
    {
        $param = $request->all();
        $param['data']['tenant_id'] = $request->getTenantId();
        $param['data']['created_by'] = $request->getCurrentUserId();
        $res = $this->item->create($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 更新项目
     * @param Request $request
     * @return \support\Response
     */
    public function update(Request $request)
    {
        $param = $request->all();
        $param['data']['tenant_id'] = $request->getTenantId();
        $param['data']['created_by'] = $request->getCurrentUserId();
        $res = $this->item->update($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 删除项目
     * @param Request $request
     * @return \support\Response
     * @throws \Throwable
     */
    public function delete(Request $request)
    {
        $param = $request->all();
        try {
            $res = $this->item->delete($param['param']['filter']['id']);
            return json(success_response('', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 删除项目
     * @param Request $request
     * @return \support\Response
     * @throws \exception
     */
    public function select(Request $request)
    {
        $param = $request->all();
        $res = $this->item->select($request->getCurrentUserId());
        return json(success_response('', $res));
    }

    /**
     * @param Request $request
     * @return \support\Response
     * @throws \exception
     */
    public function selectChild(Request $request, $id)
    {
        $param = $request->all();
        $res = $this->item->selectChild($id);
        return json(success_response('', $res));
    }

    /**
     * 查询单个项目
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function findByName(Request $request)
    {
        $res = $this->item->findByName($request->post());

        return json(success_response('', $res));
    }

    /**
     * 查询指定目录下得所有文章和目录
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectCatalogAndPost(Request $request)
    {
        $post = $request->post();
        $res = $this->item->selectCatalogAndPost($post['param']['filter']['item_id'], $post['param']['filter']['catalog_id'], $request->getTenantId());
        return json(success_response('', $res));
    }

    /**
     * 查找单个项目
     * @param Request $request
     * @return \support\Response
     */
    public function find(Request $request)
    {
        $param = $request->all();
        $res = $this->item->find($param['param']['filter']);
        return json(success_response('', $res));

    }

    /**
     * 获取知识本项目类型
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBookType(Request $request)
    {
        $param = $request->all();
        $param['user_id'] = $request->getCurrentUserId();
        $param['tenant_id'] = $request->getTenantId();
        $res = $this->item->getBookType($param);
        return json(success_response('', $res));
    }

    /**
     * 获取单个知识本项目类型
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function findBookType(Request $request)
    {
        $param = $request->all();
        $filter = [];
        if (!empty($param['param']['filter'])) {
            $filter = $param['param']['filter'];
        }
        $res = $this->item->findBookType($filter);
        return json(success_response('', $res));
    }


    /**
     * 创建知识本项目
     * @param Request $request
     * @return \support\Response
     * @throws \Throwable
     */
    public function createBookItem(Request $request)
    {
        $param = $request->all();
        $param['data']['tenant_id'] = $request->getTenantId();
        $param['data']['created_by'] = $request->getCurrentUserId();
        try {
            $res = $this->item->createBookItem($param['data']);
            return json(success_response('', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除知识本项目
     * @param Request $request
     * @return \support\Response
     * @throws \Throwable
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deleteBookItem(Request $request)
    {
        $param = $request->all();
        $res = $this->item->deleteBookItem($param['param']['filter']);
        return json(success_response('', $res));
    }

    /**
     * 获取知识库项目
     * @param Request $request
     * @return \support\Response
     * @throws \exception
     */
    public function getBookItem(Request $request)
    {
        $param = $request->all();
        $param['param']['user_id'] = $request->getCurrentUserId();
        $res = $this->item->getBookItem($param['param']);
        return json(success_response('', $res));
    }

}
